﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Movies.Web.Models;

namespace Movies.Web.Controllers
{
    public class ActorsController : Controller
    {
        protected readonly MoviesDb _dbActors;

        public ActorsController()
        {
            _dbActors = new MoviesDb();
        }

        //
        // GET: /Actors/

        public ActionResult Index()
        {
            var actors = _dbActors.Actors.ToList();

            return View(actors);
        }

        //
        // GET: /Actors/Details/5

        public ActionResult Details(int id)
        {
            var actor = _dbActors.Actors.Where(x => x.Id == id).SingleOrDefault();

            if (actor == null)
            {
                ViewBag.ErrorMessage = "Could not find record, please select from the list below";
                return RedirectToAction("index");
            }

            return View(actor);
        }

        //
        // GET: /Actors/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Actors/Create

        [HttpPost]
        public ActionResult Create(Actor model)
        {
            if (!ModelState.IsValid)
                return View();

            try
            {
                // TODO: Add insert logic here
                _dbActors.Actors.Add(model);
                _dbActors.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View();
            }
        }
        
        //
        // GET: /Actors/Edit/5
 
        public ActionResult Edit(int id)
        {
            var actor = _dbActors.Actors.Where(x => x.Id == id).SingleOrDefault();


            if (actor == null)
            {
                ViewBag.ErrorMesssage = "The actor must be contained within the list, please select and try again";
            }

            return View(actor);
        }

        //
        // POST: /Actors/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Actor model)
        {
            if (!ModelState.IsValid)
                return View(model);

            try
            {
                // TODO: Add update logic here
                var actor = _dbActors.Actors.Where(x => x.Id == id).SingleOrDefault();


                if (actor == null)
                {
                    ViewBag.ErrorMessage = "The actor is not valid, please select a valid actor from the list";
                    return RedirectToAction("index");
                }

                actor.FirstName = model.FirstName;
                actor.LastName = model.LastName;
                actor.DateOfBirth = model.DateOfBirth;
                _dbActors.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View(model);
            }
        }

        //
        // GET: /Actors/Delete/5
 
        public ActionResult Delete(int id)
        {
            var actor = _dbActors.Actors.Where(x => x.Id == id).SingleOrDefault();

            if (actor == null)
            {
                ViewBag.ErrorMessage = "The actor could be located, please find the correct actor and try again";
                return RedirectToAction("index");
            }

            return View(actor);
        }

        //
        // POST: /Actors/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, Actor model)
        {
            try
            {
                var actor = _dbActors.Actors.Where(x => x.Id == id).SingleOrDefault();

                if (actor == null)
                {
                    ViewBag.ErrorMessage = "The actor could not be found, the record might of already been purged";
                }

                _dbActors.Actors.Remove(actor);
                _dbActors.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
